package com.hengxinguotong.zhihuichengjian.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.MD5Util;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/20.
 */

public class ModifyActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_old_password)
    EditText et_old_password;
    @BindView(R.id.et_new_password)
    EditText et_new_password;
    @BindView(R.id.et_re_password)
    EditText et_re_password;
    @BindView(R.id.tv_submit)
    TextView tv_submit;
    @BindView(R.id.iv_back)
    HXTextView ivBack;

    private ModifyActivity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        ButterKnife.bind(this);
        mActivity = this;
    }

    @OnClick({R.id.tv_submit, R.id.iv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_submit:
                String userPassword = et_old_password.getText().toString();
                String newPassword = et_new_password.getText().toString();
                String rePassword = et_re_password.getText().toString();
                if(userPassword.equals(newPassword)){
                    showToast("新密码不能与原密码相同！");
                    return;
                }else if(!newPassword.equals(rePassword)){
                    showToast("两次输入密码不相同，请重新输入！");
                    return;
                }
                mofify("正在提交…");
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void mofify(String alert){
        JSONObject jsonObject = new JSONObject();
//        String userName = et_username.getText().toString();
        String userName = SPUtil.getString(mActivity, "userName");
        String userPassword = et_old_password.getText().toString();
        String newPassword = et_new_password.getText().toString();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("userPassword", MD5Util.getMD5(userName + userPassword));
            jsonObject.put("newPassword", MD5Util.getMD5(userName + newPassword));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, mActivity, "/user/modifyPassword/" + SPUtil.getString(mActivity, "uuid"),
                params, new Utils.ListenerJson() {
                    @Override
                    public void onSuccess(String json) {
                        try {
                            JSONObject object = new JSONObject(json);
                            String status = object.getString("status");//接口调用标识.1:成功; 0:失败。
                            String msg = object.getString("msg");
                            if("1".equals(status)){
                                showToast("恭喜您，修改密码成功！");
                                finish();
                            }else if("0".equals(status)){
                                showToast("修改密码失败，请稍后重试。");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(String json) {
//                        showToast("修改密码失败，请稍后重试。");
                        showToast(json);
                    }
                });
    }
}
